//
//  InputTableViewCell.swift
//  HuaYun
//
//  Created by enghou on 2018/11/20.
//  Copyright © 2018 xyxorigation. All rights reserved.
//

import UIKit

class InputTableViewCell: UITableViewCell,UITextFieldDelegate {

    var contentLabel: UITextField!
    var titleLbl: UILabel!
    var key: String = ""
    var value: String {
        get{
            return contentLabel.text ?? ""
        }
    }
    var param: Dictionary<String,String>{
        get{
            return [key:value]
        }
    }
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        titleLbl = UILabel()
        titleLbl.textColor = UIColor.hex("333333")
        titleLbl.font = UIFont.boldSystemFont(ofSize: 18)
        self.contentView.addSubview(titleLbl)
        titleLbl.snp.makeConstraints { (make) in
            make.left.equalTo(16)
            make.top.equalToSuperview().offset(8)
        }
        contentLabel = UITextField()
        self.contentView.addSubview(contentLabel)
        contentLabel.placeholder = "请输入"
        contentLabel.delegate = self
        contentLabel.font = UIFont.systemFont(ofSize: 15)
        contentLabel.snp.makeConstraints { (make) in
            make.left.equalTo(titleLbl.snp.left).offset(5)
            make.top.equalTo(titleLbl.snp.bottom).offset(0)
            make.right.equalToSuperview().offset(-20)
            make.height.equalTo(30)
        }
    }
    var isnumber = false
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if string == "" {
            return true
        }
        if string.count > 1 {
            if self.isnumber {
                return false
            } else {
                return true
            }
        }
        if self.isnumber {
            if string != "." {
                let val = Double.init(string)
                if val == nil {
                    return false
                }
            }
        }
        if string == "." {
            if (textField.text?.contains(".") ?? false) == true {
                return false
            } else {
                return true
            }
        }
        if (textField.text?.contains(".") ?? false) == true {
            let distance = textField.text!.distance(from: textField.text!.range(of: ".")!.upperBound, to: textField.text!.endIndex)
            if distance < 2 {
                return true
            } else {
                return false
            }
        } else {
            return true
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func prepareForReuse() {
        super.prepareForReuse()
    }
    
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
